#!/bin/bash 


StopJboss () {
    echo -n "Stoping jboss: "
    /etc/init.d/jboss stop	>> ${LogFile} 2>&1

    checkIfJbossIsRunning=`ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' `
    if [ "x${checkIfJbossIsRunning}" != "x" ] ; then
	sleep 5
	kill -9 ${checkIfJbossIsRunning}
    fi       

    echo "ok"

}



#### start jboss 

StartJboss () {
    echo -n "Starting jboss: "
    /etc/init.d/jboss start	>> ${LogFile} 2>&1
    echo "ok"

}

######## ejbca deploy ######
EjbcaDeploy() {
    echo -n "Running ejbca deploy: "
    
    cd ${EJBCA_HOME}

    su -p -s /bin/sh jboss -c "ant deploy" 
    if [ $? -eq 0 ] ; then
	echo "Step ejbca deploy: ok"
    else
	echo
	echo "FAILED: ejbca deploy failed"
	exit 5
    fi
}



ActivateCa(){
    
    echo "to activate CA:s run: ${SMARTCARD20_HOME}/activate-cas.sh"
    echo 
    exit 0
}


#"main"


LogFile="/tmp/ejbca-build-and-deploy.$$"
PATHENV=/etc/certificate-services.conf
if [ -r ${PATHENV} ] ; then
    source ${PATHENV}
else
    echo "${PATHENV} missing"
    exit
fi


echo "Welcome to $0, a tool to upgrade EJBCA"
echo "To view verbose install messages: tail -f ${LogFile}"

runningid=`id -u`
if [ "x${runningid}" != "x0" ] ; then
    echo "must run as root eg: sudo $0"
    exit
fi



StopJboss
sleep 10
EjbcaDeploy
StartJboss

ActivateCa
